<?php

/**
 * Description of Application_Model_Fields_File
 *
 * @author Jean Brito
 */
class Application_Model_Fields_File extends Application_Model_Fields_Abstract{
	//protected $_sqlType='longblob';
	//protected $_sqlSize=null;
	
	protected $_url='data/uploads';
	public function __construct($model,$setup){
		parent::__construct($model,$setup);
		if(isset($setup['url']))$this->_url=$setup['url'];
	}
	
	public function setValue(&$row,$value=''){
		if(empty($value['name']))return $row;
		$fd=explode('/',trim($this->_url.'/'.$this->getModel()->getTableName().'/'.$this->_name,"/"));
		$file="./";
		foreach($fd as $d=>$dir){
			$file.=$dir;
			if(!is_dir($file)&&$d<(count($fd))){
				mkdir($file);
			}
			$file.="/";
		}
		
		$fo=fopen($file.$value['name'],'w');
		fwrite($fo,file_get_contents($value['tmp_name']));
		fclose($fo);
		
		$row[$this->_name]=$file.$value['name'];
		return $row;
	}
	
	
	
}
